varying vec2 pointAngle;
// ----------------------------------------------------------------------------
uniform float skyBrightness;
uniform vec3 sunVector;
uniform vec3 topColor;
uniform vec3 bottomColor;
uniform vec3 hazeColor;

uniform float sunCoverAngle;
uniform float haloCoverAngle;
uniform float sunIntensityFactor;
uniform float sunSmoothBorder;
uniform float hazeStrength;
uniform float hazeAmmount;


/*
const vec3 topColor = 2.0*vec3(0.015,0.027,0.07); //vec3( 0.42, 0.40, 0.80);
const vec3 bottomColor = 7.0*vec3(0.037,0.082,0.147);//vec3( 0.94, 0.90, 1.00);
const vec3 hazeColor = vec3( 0.50, 0.50, 0.50);

const float sunCoverAngle= 0.6*3.141592/180.0;
const float haloCoverAngle = 1.2*3.141592/180.0;
const float sunIntensityFactor = 10.0;
const float sunSmoothBorder = 4.0;
const float hazeStrength = 10.0;
const float hazeAmmount = 0.3;
*/
// ----------------------------------------------------------------------------

void main()
{
	vec3 pointVector;
	pointVector.y = sin(pointAngle.y)/3.1415926535;
	pointVector.x = cos(pointAngle.x)/3.1415926535;
	pointVector.z = sin(pointAngle.x)/3.1415926535;
	vec3 sunV = sunVector/length(sunVector);

	float smoothDist = smoothstep( haloCoverAngle, sunCoverAngle, acos(dot(normalize(pointVector),normalize(sunV))));
	float sunIntensity = pow(smoothDist, sunSmoothBorder);

	float upIntensity = (pointAngle.y)/3.1415926535+3.1415926535*0.5;

	//Add an horizon haze
	float haze = pow(pointAngle.y+1.0, hazeStrength);
	haze = max(0.0, pow(2.71828, -10.0*haze))*hazeAmmount;

	vec3 skyColor = mix(bottomColor,topColor,upIntensity);
	//if(mod(pointAngle.r*30.0,2.0) >0.9 && mod(pointAngle.g*30.0,2.0)>0.9)
	//	discard;
	skyColor =skyBrightness * mix( skyColor,hazeColor,haze );


	gl_FragColor.rgb = max(vec3(0.0), sunIntensityFactor*vec3(1.82,1.26,0.91) * sunIntensity + skyColor);

	gl_FragColor.a = 1.0;
}

